package com.ogawa.base.utils

import android.annotation.SuppressLint
import android.content.Context
import android.text.TextUtils
import com.blankj.utilcode.util.LogUtils
import com.google.gson.Gson
import com.ogawa.base.base.BaseApp

open class SpUtil {
    companion object {
        private val FILE_NAME = BaseApp.appContext.packageName + ".preference"
//        const val USER_TOKEN = "user_token"
        const val LANGUAGE = "language"
        const val SN = "sn"
        const val IS_NEED_RE_CONNECT_MQTT = "IS_NEED_RE_CONNECT_MQTT"
        private val mContext = BaseApp.appContext

        /**
         * 保存数据的方法，我们需要拿到保存数据的具体类型，然后根据类型调用不同的保存方法
         *
         * @param key
         * @param obj
         */
        @SuppressLint("CommitPrefEdits")
        fun set(key: String, obj: Any) {
            val sp = mContext.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
            val edit = sp.edit()
            when (obj) {
                is String -> {
                    edit.putString(key, obj)
                }
                is Int -> {
                    edit.putInt(key, obj)
                }
                is Boolean -> {
                    edit.putBoolean(key, obj)
                }
                is Float -> {
                    edit.putFloat(key, obj)
                }
                is Long -> {
                    edit.putLong(key, obj)
                }
                else -> {
                    edit.putString(key, obj.toString())
                }
            }
            edit.apply()
        }

        /**
         * 保存Model（Model里面可以嵌套Model与List）
         * 说明：建议在线程中调用此函数
         *
         * @param key
         * @param obj
         * @return
         */
        fun setObject(key: String?, obj: Any?) {
            var jsonValue = ""
            if (obj != null) {
                val gson = Gson()
                jsonValue = gson.toJson(obj)
            }
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            val editor = sp.edit()
            editor.putString(key, jsonValue)
            editor.apply()
        }

        /**
         * 得到保存数据的方法，我们根据默认值得到保存的数据的具体类型，然后调用相对于的方法获取值
         *
         * @param key
         * @param defaultObject
         * @return
         */
        fun get(key: String?, defaultObject: Any?): Any? {
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            when (defaultObject) {
                is String -> {
                    return sp.getString(key, defaultObject as String?)
                }
                is Int -> {
                    return sp.getInt(key, (defaultObject as Int?)!!)
                }
                is Boolean -> {
                    return sp.getBoolean(key, (defaultObject as Boolean?)!!)
                }
                is Float -> {
                    return sp.getFloat(key, (defaultObject as Float?)!!)
                }
                is Long -> {
                    return sp.getLong(key, (defaultObject as Long?)!!)
                }
                else -> return null
            }
        }

        /**
         * 通过key获取Model
         * 说明：建议在线程中调用此函数
         *
         * @param key
         * @param clazz
         * @return
         */
        fun <E> getObject(key: String?, clazz: Class<E>?): E? {
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            val jsonValue = sp.getString(key, null)
            if (!TextUtils.isEmpty(jsonValue)) {
                val gson = Gson()
                return gson.fromJson(jsonValue, clazz)
            }
            return null
        }

        /**
         * 移除某个key值已经对应的值
         *
         * @param key
         */
        fun remove(key: String?) {
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            val editor = sp.edit()
            editor.remove(key)
            editor.apply()
        }

        /**
         * 清除所有数据
         */
        fun clear() {
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            val editor = sp.edit()
            editor.clear()
            editor.apply()
        }

        /**
         * 查询某个key是否已经存在
         *
         * @param key
         * @return
         */
        fun contains(key: String?): Boolean {
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            return sp.contains(key)
        }

        /**
         * 返回所有的键值对
         *
         * @return
         */
        fun getAll(): Map<String?, *>? {
            val sp = mContext.getSharedPreferences(
                FILE_NAME,
                Context.MODE_PRIVATE
            )
            return sp.all
        }
    }
}